package com.dk.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dk.data.Result;
import com.dk.em.ConstantEnum;
import com.dk.pojo.TContent;
import com.dk.pojo.TContentPrice;
import com.dk.service.ITContentPriceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 广告价格表 前端控制器
 * </p>
 *
 * @author author
 * @since 2021-10-29
 */
@RestController
@RequestMapping("/t-content-price")
@Api(value = "广告价格管理")
public class TContentPriceController {

    @Autowired
    private ITContentPriceService priceService;

    /*@ApiOperation(value = "广告价格查询接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "categoryId",value = "广告类型名称",required = true),
            @ApiImplicitParam(name = "contentSort",value = "广告类型排序",required = true),
            @ApiImplicitParam(name = "startTime",value = "广告开始时间",required = true),
            @ApiImplicitParam(name = "endTime",value = "广告结束时间",required = true)
    })
    @GetMapping("/findInfo")
    public Result findInfo(Integer categoryId, Integer contentSort,TContent content){
        System.out.println(content.getStartTime());
        TContentPrice price = new TContentPrice();
        QueryWrapper Wrapper = new QueryWrapper<TContentPrice>();
        Wrapper.eq("categoryId",categoryId);
        Wrapper.eq("contentSort",contentSort);
        TContentPrice contentPrice = priceService.getOne(Wrapper);

        long start = content.getStartTime().getTime();
        long end = content.getEndTime().getTime();
        long time = Math.abs((end - start) / 24 * 3600 * 1000);
        int day = (int)time;

        BigDecimal p = new BigDecimal(day);
        BigDecimal decimal = p.multiply(price.getPrice());
        price.setPrice(decimal);
        return Result.bulid(ConstantEnum.SUCCESS).setResult(contentPrice);
    }*/

    @PostMapping("/{categoryId}/{contentSort}/{price}")
    @ApiOperation(value = "广告价格增加接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "categoryId",value = "广告类型id",required = true,paramType = "path"),
            @ApiImplicitParam(name = "contentSort",value = "广告类型排序",required = true,paramType = "path"),
            @ApiImplicitParam(name = "price",value = "广告价格",required = true,paramType = "path")
    })
    public Result add(@PathVariable("categoryId") Integer categoryId,@PathVariable("contentSort") Integer contentSort,
                      @PathVariable("price") BigDecimal price){
        TContentPrice price1 = new TContentPrice();
        price1.setCategoryId(categoryId);
        price1.setContentSort(contentSort);
        price1.setPrice(price);
        priceService.save(price1);
        return Result.bulid(ConstantEnum.SUCCESS);
    }

    @PutMapping("/{id}/{categoryId}/{contentSort}/{price}")
    @ApiOperation(value = "广告价格修改接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "广告价格id",required = true,paramType = "path"),
            @ApiImplicitParam(name = "categoryId",value = "广告类型id",required = true,paramType = "path"),
            @ApiImplicitParam(name = "contentSort",value = "广告类型排序",required = true,paramType = "path"),
            @ApiImplicitParam(name = "price",value = "广告价格",required = true,paramType = "path")
    })
    public Result update(@PathVariable("id") Long id,@PathVariable("categoryId") Integer categoryId,@PathVariable("contentSort") Integer contentSort,
                         @PathVariable("price") BigDecimal price){
        TContentPrice contentPrice = new TContentPrice();
        contentPrice.setId(id);
        contentPrice.setCategoryId(categoryId);
        contentPrice.setContentSort(contentSort);
        contentPrice.setPrice(price);
        priceService.updateById(contentPrice);
        return Result.bulid(ConstantEnum.SUCCESS);
    }

    @DeleteMapping("/{ids}")
    @ApiOperation(value = "广告价格批量删除接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids",value = "广告价格id",required = true,paramType = "path")
    })
    public Result delete(@PathVariable("ids") Long[] ids){
        priceService.removeByIds(Arrays.asList(ids));
        return Result.bulid(ConstantEnum.SUCCESS);
    }

    @RequestMapping(value = "findAll",method = RequestMethod.GET)
    @ApiOperation(value = "广告价格全部查询接口")
    public Result findAll(){
        List<TContentPrice> list = priceService.list();
        return Result.bulid(ConstantEnum.SUCCESS).setResult(list);
    }
}
